<?php defined('SYSPATH') or die('No direct script access.');
                          
class Ajax_Controller extends Controller 
{
	public function checkusername($username)
	{
		$result = Database::instance()->query("SELECT id FROM customers WHERE username='".$username."'");
		
		if( count($result) > 0 )
		{
			echo '$("reg_username").className = "red-box";';
			echo "$('__ErrorMessagePanel_username').innerHTML = '".html::image('images/check_error.gif').' '.Kohana::lang('site.input_msg_username_used')."';";
			echo '$("reg_username").focus();';
		}
		else
		{
			echo '$("reg_username").className = "";';
			echo "$('__ErrorMessagePanel_username').innerHTML = '".html::image('images/check_right.gif')."';";
		}
	}
		
	public function checkuseremail($email)
	{
		$uri = $_SERVER['REQUEST_URI'];
		
		$arr = explode("?", $uri);
		$arr = explode("/", $arr[0]);
		$email = $arr[count($arr)-1];
		
		$result = Database::instance()->query("SELECT id FROM customers WHERE email='".$email."'");
		
		echo 'count = "'.$email.'";';
		
		if( count($result) > 0 )
		{
			echo '$("reg_email").className = "red-box";';
			echo "$('__ErrorMessagePanel_email').innerHTML = '".html::image('images/check_error.gif').' '.Kohana::lang('site.input_msg_useremail_used')."';";
			echo '$("reg_email").focus();';
		}
		else
		{
			echo '$("reg_email").className = "";';
			echo "$('__ErrorMessagePanel_email').innerHTML = '".html::image('images/check_right.gif')."';";
		}
	}
		
	public function getzones($code, $type)
	{
		$result = Database::instance()->query('SELECT * FROM zones WHERE country_id='.$code);
		
		echo '<select id="'.$type.'_zone_id" name="'.$type.'_zone_id">';
		
		foreach($result as $item)
		{
			echo '<option value="'.$item->id.'">'.$item->name.'</option>';
		}
		
		echo '</select>';
	}

	public function getshippingfare($dest_postal, $dest_city, $dest_country, $service_type, $weight)
	{
		$ResCode = myfuns::get_ups_shipping_fare($dest_postal, $dest_city, $dest_country, $service_type, $weight);
		
		if(is_numeric($ResCode))
		{
			echo '$("shipping_fare").value = "'.$ResCode.'";';
			echo '$("genorder").disabled = false;';
		}
		else
		{
			echo 'alert("'.Kohana::lang('site.input_msg_ponumber_used').$ResCode.'");';
		}
	}
	
	public function checkponumber($ponumber)
	{
		$result = Database::instance()->query("SELECT id FROM customers WHERE po_number='".$ponumber."' AND id<>".cookie::get('userid'));
		
		if( count($result) > 0 )
		{
			echo '$("customer_po_number").className = "red-box";';
			echo '$("__ErrorMessagePanel").innerHTML = "'.Kohana::lang('site.input_msg_ponumber_used').'";';
			echo '$("customer_po_number").focus();';
		}
		else
		{
			echo '$("customer_po_number").className = "";';
			echo '$("__ErrorMessagePanel").innerHTML = "";';
		}
	}
}